import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

import { login, logincb } from './login'
import SearchView from '@/views/SearchView.vue'
import BasicConsoleView from '@/views/BasicConsoleView.vue'
import ScreenConsoleView from '@/views/ScreenConsoleView.vue'
import OptfastConsoleView from '@/views/OptfastConsoleView.vue'
import LabConsoleView from '@/views/LabConsoleView.vue'
import SearchUrlView from '@/views/SearchUrlView.vue'
import { isLogin } from '@/utils/user'



const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'search',
    component: SearchView
  },
  {
    path: '/basicConsole',
    name: 'console',
    component: BasicConsoleView,
    meta: {
      needLogin: true,
    }
  },
  {
    path: '/screenConsole',
    name: 'screenConsole',
    component: ScreenConsoleView,
    meta: {
      needLogin: true,
    }
  },
  {
    path: '/optfastConsole',
    name: 'optfastConsole',
    component: OptfastConsoleView,
    meta: {
      needLogin: true,
    }
  }
  ,
  {
    path: '/labConsole',
    name: 'labConsole',
    component: LabConsoleView,
    meta: {
      needLogin: true,
    }
  },
  {
    path: '/search',
    name: 'searchUrl',
    component: SearchUrlView,
    meta: {
      needLogin: true,
    }
  },
  { path: '/login', name: 'login', beforeEnter: login, component: BasicConsoleView },
  { path: '/login-cb', name: 'logincb', beforeEnter: logincb, component: BasicConsoleView },

]



const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, _, savedPosition) {
    if (savedPosition) {
      /** 处理前进后退滚动条到先前位置 */
      return savedPosition
    } else if (to.hash) {
      /** 处理带锚点的链接到页面锚点位置 */
      return {
        /** 其他情况将滚动条回到顶部 */
        el: to.hash,
        behavior: 'smooth',
      }
    } else {
      return { top: 0 }
    }
  },
})

router.beforeEach((to) => {
  if (!isLogin() && to.meta.needLogin) {
    router.push("/login?jump=" + to.fullPath)
    
  }
})



export default router
